package com.aw.baselib.util

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.aw.baselib.R
import com.aw.baselib.util.AwDataUtil.isEmpty
import com.aw.baselib.util.AwDisplayUtil.dip2px
import com.aw.baselib.util.AwLog.d
import com.aw.baselib.widgets.AwRecycleViewDivider
import com.aw.baselib.widgets.AwRecyclerViewNoBugLinearLayoutManager
import com.aw.baselib.widgets.AwViewCustomDivider
import com.aw.baselib.widgets.SpaceItemDecoration
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * Created by hzw
 */
object AwRecyclerViewUtil {
    fun resetView(
        activity: Activity,
        adapter: BaseQuickAdapter<*, *>,
        recyclerView: RecyclerView,
        emptyTxt: String?
    ) {
        adapter.setNewData(null)
        recyclerView.removeAllViews()
        adapter.setHeaderAndEmpty(true)
        adapter.emptyView = getEmptyDataView(activity, emptyTxt, -1)
    }

    fun resetView(activity: Activity, adapter: BaseQuickAdapter<*, *>, recyclerView: RecyclerView) {
        adapter.setNewData(null)
        recyclerView.removeAllViews()
        adapter.setHeaderAndEmpty(true)
        adapter.emptyView = getEmptyDataView(
            activity,
            activity.resources.getString(R.string.common_no_data),
            -1
        )
    }

    fun resetView(
        activity: Activity,
        adapter: BaseQuickAdapter<*, *>,
        recyclerView: RecyclerView,
        onClickListener: View.OnClickListener?
    ) {
        adapter.setNewData(null)
        recyclerView.removeAllViews()
        adapter.setHeaderAndEmpty(true)
        val emptyView =
            getEmptyDataView(activity, activity.resources.getString(R.string.common_no_data), -1)
        if (onClickListener != null) {
            emptyView.setOnClickListener(onClickListener)
        }
        adapter.emptyView = emptyView
    }

    fun resetView(
        activity: Activity,
        adapter: BaseQuickAdapter<*, *>,
        recyclerView: RecyclerView,
        resetView: Boolean
    ) {
        if (resetView) {
            adapter.setNewData(null)
            recyclerView.removeAllViews()
            adapter.setHeaderAndEmpty(true)
            adapter.emptyView = getEmptyDataView(
                activity,
                activity.resources.getString(R.string.common_no_data),
                -1
            )
        }
    }

    fun resetView(
        activity: Activity,
        adapter: BaseQuickAdapter<*, *>,
        recyclerView: RecyclerView,
        resetView: Boolean,
        onClickListener: View.OnClickListener?
    ) {
        if (resetView) {
            adapter.setNewData(null)
            recyclerView.removeAllViews()
            adapter.setHeaderAndEmpty(true)
            val emptyView = getEmptyDataView(
                activity,
                activity.resources.getString(R.string.common_no_data),
                -1
            )
            if (onClickListener != null) {
                emptyView.setOnClickListener(onClickListener)
            }
            adapter.emptyView = emptyView
        }
    }

    /**
     * 通用 SwipeRefreshLayout 设置
     * @param srlLayout
     * @param isEnable
     */
    fun setSwipeRefreshLayout(srlLayout: SwipeRefreshLayout, isEnable: Boolean) {
        srlLayout.setColorSchemeResources(
            android.R.color.holo_blue_bright, android.R.color.holo_green_light,
            android.R.color.holo_orange_light, android.R.color.holo_red_light
        )
        srlLayout.setDistanceToTriggerSync(300)
        srlLayout.setSize(SwipeRefreshLayout.DEFAULT)
        srlLayout.isEnabled = isEnable
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     */
    fun setRecyclerViewLinearlayoutHorizontal(
        activity: Activity?,
        recyclerView: RecyclerView,
        mAdapter: BaseQuickAdapter<*, *>?
    ) {
        val layoutManager = AwRecyclerViewNoBugLinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = mAdapter
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    fun setRecyclerViewLinearlayout(
        activity: Activity,
        recyclerView: RecyclerView,
        mAdapter: BaseQuickAdapter<*, *>?,
        showItemDecoration: Boolean
    ) {
        val layoutManager = AwRecyclerViewNoBugLinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        if (showItemDecoration) {
            recyclerView.addItemDecoration(
                AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    dip2px(activity, 0.5f), activity.resources.getColor(R.color.color_ebebeb)
                )
            )
        }
        //        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.adapter = mAdapter
    }

    fun setRecyclerViewLinearlayoutNoAnimation(
        activity: Activity,
        recyclerView: RecyclerView,
        mAdapter: BaseQuickAdapter<*, *>?,
        showItemDecoration: Boolean
    ) {
        val layoutManager = AwRecyclerViewNoBugLinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        if (showItemDecoration) {
            recyclerView.addItemDecoration(
                AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    dip2px(activity, 0.5f), activity.resources.getColor(R.color.color_ebebeb)
                )
            )
        }
        recyclerView.adapter = mAdapter
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    fun setRecyclerViewLinearlayoutJudgeAutoSetAdapter(
        activity: Activity,
        recyclerView: RecyclerView,
        mAdapter: BaseQuickAdapter<*, *>?,
        showItemDecoration: Boolean,
        autoSetAdapter: Boolean
    ) {
        val layoutManager = AwRecyclerViewNoBugLinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        if (showItemDecoration) {
            recyclerView.addItemDecoration(
                AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    dip2px(activity, 0.5f), activity.resources.getColor(R.color.color_ebebeb)
                )
            )
        }
        //        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if (autoSetAdapter) {
            recyclerView.adapter = mAdapter
        }
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param decorationHeightF
     * @param autoSetAdapter
     */
    fun setRecyclerViewLinearlayoutWithDecorationHeight(
        activity: Activity, recyclerView: RecyclerView, mAdapter: BaseQuickAdapter<*, *>?,
        decorationHeightF: Float, autoSetAdapter: Boolean
    ) {
        val layoutManager = AwRecyclerViewNoBugLinearLayoutManager(activity)
        layoutManager.orientation =
            LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        recyclerView.addItemDecoration(
            AwRecycleViewDivider(
                activity,
                LinearLayoutManager.HORIZONTAL,
                dip2px(activity, decorationHeightF),
                activity.resources.getColor(R.color.color_ebebeb)
            )
        )
        //        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
//        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if (autoSetAdapter) recyclerView.adapter = mAdapter
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param mAdapter
     * @param showItemDecoration
     */
    fun setRecyclerViewLinearlayout(
        activity: Activity,
        recyclerView: RecyclerView,
        mAdapter: BaseQuickAdapter<*, *>?,
        layoutManager: LinearLayoutManager,
        showItemDecoration: Boolean,
        autoSetAdapter: Boolean
    ) {
        //        RecyclerViewNoBugLinearLayoutManager layoutManager = new RecyclerViewNoBugLinearLayoutManager(activity);
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        if (showItemDecoration) {
            recyclerView.addItemDecoration(
                AwRecycleViewDivider(
                    activity, LinearLayoutManager.HORIZONTAL,
                    dip2px(activity, 0.5f), activity.resources.getColor(R.color.color_ebebeb)
                )
            )
        }
        //        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        if (autoSetAdapter) recyclerView.adapter = mAdapter
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param adapter
     */
    fun setRecyclerViewGridlayout(
        activity: Activity?,
        recyclerView: RecyclerView,
        adapter: BaseQuickAdapter<*, *>?,
        spanCount: Int
    ) {
        val manager = GridLayoutManager(activity, spanCount)
        //        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        manager.supportsPredictiveItemAnimations();
        recyclerView.layoutManager = manager
        //        recyclerView.addItemDecoration((new AwViewCustomDivider(AwDisplayUtil.dip2px(activity, 5))));
//        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.adapter = adapter
    }

    /**
     * 通用recyclerview设置
     * @param activity
     * @param recyclerView
     * @param adapter
     */
    fun setRecyclerViewGridlayout(
        activity: Activity?,
        recyclerView: RecyclerView,
        adapter: BaseQuickAdapter<*, *>?,
        spanCount: Int,
        dividerValue: Int
    ) {
        val manager = GridLayoutManager(activity, spanCount)
        //        manager.setOrientation(LinearLayoutManager.VERTICAL);
//        manager.supportsPredictiveItemAnimations();
        recyclerView.layoutManager = manager
        if (recyclerView.getTag(recyclerView.id) != null && recyclerView.getTag(recyclerView.id) == "addedDecoration") {
        } else {
            recyclerView.addItemDecoration(
                SpaceItemDecoration(
                    dip2px(
                        activity!!, dividerValue.toFloat()
                    ), spanCount
                )
            )
            recyclerView.setTag(recyclerView.id, "addedDecoration")
        }
        //        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.adapter = adapter
    }

    /**
     * 根据item长度动态控制每行item数量
     * @param activity
     * @param manager
     * @param recyclerView
     * @param adapter
     * @param dividerValue
     */
    fun setRecyclerViewGridlayout(
        activity: Activity?,
        manager: GridLayoutManager?,
        recyclerView: RecyclerView,
        adapter: BaseQuickAdapter<*, *>?,
        dividerValue: Int
    ) {
//        manager.supportsPredictiveItemAnimations();
        recyclerView.layoutManager = manager
        recyclerView.addItemDecoration(
            AwViewCustomDivider(
                dip2px(
                    activity!!, dividerValue.toFloat()
                )
            )
        )
        //        DefaultItemAnimator animator = new DefaultItemAnimator();
//        recyclerView.setItemAnimator(animator);
        recyclerView.adapter = adapter
    }

    /**
     * 空数据页面展示
     * @param context
     * @param tag 0 未定时间 1 发起/参与  2 无信息 3 无数据
     * @return
     */
    fun getEmptyDataView(context: Context, tag: Int, drawableId: Int): View {
        d("getEmptyDataView tag: $tag")
        val emptyView = LayoutInflater.from(context).inflate(R.layout.view_no_data, null)
        val tv_noData = emptyView.findViewById<View>(R.id.tv_noData) as TextView
        if (-1 != drawableId) {
            setDrawableDirection(context, tv_noData, 3, drawableId)
        }
        when (tag) {
            else -> tv_noData.text = context.resources.getString(R.string.common_no_data)
        }
        emptyView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return emptyView
    }

    /**
     * 空数据页面展示
     * @param context
     * @return
     */
    fun getEmptyDataView(context: Context, noDataTxt: String?, drawableId: Int): View {
        var noDataTxt = noDataTxt
        if (isEmpty(noDataTxt)) {
            noDataTxt = context.resources.getString(R.string.common_no_data)
        }
        val emptyView = LayoutInflater.from(context).inflate(R.layout.view_no_data, null)
        val tv_noData = emptyView.findViewById<View>(R.id.tv_noData) as TextView
        if (-1 != drawableId) {
            setDrawableDirection(context, tv_noData, 3, drawableId)
        }
        tv_noData.text = noDataTxt
        emptyView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return emptyView
    }

    /**
     * 左1右2上3下4
     */
    fun setDrawableDirection(context: Context, tv: TextView, tag: Int, resId: Int) {
        val drawable = context.resources.getDrawable(resId)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        when (tag) {
            1 -> tv.setCompoundDrawables(drawable, null, null, null)
            2 -> tv.setCompoundDrawables(null, null, drawable, null)
            3 -> tv.setCompoundDrawables(null, drawable, null, null)
            4 -> tv.setCompoundDrawables(null, null, null, drawable)
        }
    }

    fun cancelDrawableDirection(context: Context?, tv: TextView) {
        tv.setCompoundDrawables(null, null, null, null)
    }
}